package kaizen.jdbc;

import java.io.PrintStream;

import java.sql.*;

import java.util.Vector;

// Referenced classes of package kaizen.jdbc:
//            dbCursor, dbRecord

public class dbCon {

    public dbCon(String s, Connection connection1, boolean flag) {
        ls_name = "";
        lb_status = false;
        vCursor = new Vector();
        ls_name = s;
        connection = connection1;
        lb_status = flag;
        try {
            connection.setAutoCommit(false);
            connection1.close();
            connection.close();
            return;
        } catch (SQLException sqlexception) {
            System.out.println("connection.setAutoCommit " + sqlexception);
        }
    }

    public void First(String s) {
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (dbcursor.getName().equals(s)) {
                dbcursor.First();
                return;
            }
        }

    }

    public void Last(String s) {
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (dbcursor.getName().equals(s)) {
                dbcursor.Last();
                return;
            }
        }

    }

    public void Next(String s) {
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (dbcursor.getName().equals(s)) {
                dbcursor.Next();
                return;
            }
        }

    }

    public void Previous(String s) {
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (dbcursor.getName().equals(s)) {
                dbcursor.Previous();
                return;
            }
        }

    }

    public void addCursor(dbCursor dbcursor) {
        vCursor.addElement(dbcursor);
    }

    public void close(String s) {
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (dbcursor.getName().equals(s)) {
                try {
                    dbcursor.results.close();
                    
                } catch (Exception e) {
                    System.out.println("no se pudo cerrar el resultset " + e);
                }
                vCursor.removeElementAt(i);

                return;

            }
        }

    }

    public void commit() {
        try {
            connection.commit();
            return;
        } catch (SQLException sqlexception) {
            System.out.println("Error connection.commit " + sqlexception);
        }
    }

    public Vector getAllRecords(String s) {
        Vector vector = null;
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (!dbcursor.getName().equals(s)) {
                continue;
            }
            vector = dbcursor.getAllRecords();
            break;
        }

        return vector;
    }

    public String[] getAllRows(String s) {
        String as[] = null;
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (!dbcursor.getName().equals(s)) {
                continue;
            }
            as = dbcursor.getAllRows();
            break;
        }

        return as;
    }

    public Connection getConnection() {
        return connection;
    }

    public String getName() {
        return ls_name;
    }

    public dbRecord getRecord(String s) {
        dbRecord dbrecord = null;
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (!dbcursor.getName().equals(s)) {
                continue;
            }
            dbrecord = dbcursor.getRecord();
            break;
        }

        return dbrecord;
    }

    public ResultSet getResults(String s) {
        ResultSet resultset = null;
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (!dbcursor.getName().equals(s)) {
                continue;
            }
            resultset = dbcursor.getResults();
            break;
        }


        return resultset;

    }

    public ResultSetMetaData getResultsMD(String s) {
        ResultSetMetaData resultsetmetadata = null;
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (!dbcursor.getName().equals(s)) {
                continue;
            }
            resultsetmetadata = dbcursor.getResultsMD();
            break;
        }

        return resultsetmetadata;
    }

    public String getRow(String s) {
        String s1 = null;
        Object obj = null;
        for (int i = 0; i < vCursor.size(); i++) {
            dbCursor dbcursor = (dbCursor) vCursor.elementAt(i);
            if (!dbcursor.getName().equals(s)) {
                continue;
            }
            s1 = dbcursor.getRow();
            break;
        }

        return s1;
    }

    public boolean getStatus() {
        try {
            if (connection.isClosed()) {
                lb_status = false;
            }
        } catch (SQLException sqlexception) {
            System.out.println("Error connection.isClosed " + sqlexception);
        }
        return lb_status;
    }

    public void rollback() {
        try {
            connection.rollback();
            return;
        } catch (SQLException sqlexception) {
            System.out.println("Error connection.rollback " + sqlexception);
        }
    }

    public void setStatus(boolean flag) {
        lb_status = flag;
    }
    private String ls_name;
    private Connection connection;
    private boolean lb_status;
    public Vector vCursor;
}
